function PlotResultsCombP( electrNr, subjectNrs )
% plot alle periodes (4 verschillende lijnen) in 1 figure. 
load('Output.mat');

electrodes = GetElectrodes();
if nargin >= 1
    electrodes = electrodes(electrNr);
end

if nargin < 2
    subjectNrs = 1:27;
end

PlotResultsByCompEP(output, subjectNrs, electrodes);
end





%% PlotResults
function PlotResultsByCompEP(output, subjectNrs, electrodes)

% skipnext = true;
disp(['====> Subjects:']);
for i = subjectNrs
    disp(['     ' output.subjects(i).Name]);
end
%     if skipnext; continue; end;
for e=1:length(electrodes)
    electrode = electrodes{e};
    
        f = PlotMultiplePeriods(output, subjectNrs, electrode);
  
        saveFigure(f, 'J:\Esther\PeakAnalysis\ResultsWithoutErrorbar\', ['AllPeriods - ' electrode]);
        close(f);
   
end
end


function fig = PlotMultiplePeriods(output, subjectNrs, electrode)
% Loop over periodes
   % Haal de data op met ResultsEP voor alle 4 periodes
   [D1 X1 D1i X1i] = ResultsEP2(output, subjectNrs, electrode, 1);
   D1mean = mean(D1i,3);
   D1std = std(D1i, 0, 3);
   
   [D2 X2 D2i X2i] = ResultsEP2(output, subjectNrs, electrode, 2);
   D2mean = mean(D2i,3);
   D2std = std(D2i, 0, 3);

   [D3 X3 D3i X3i] = ResultsEP2(output, subjectNrs, electrode, 3);
   D3mean = mean(D3i,3);
   D3std = std(D3i, 0, 3);

   [D4 X4 D4i X4i] = ResultsEP2(output, subjectNrs, electrode, 4);
   D4mean = mean(D4i,3);
   D4std = std(D4i, 0, 3);

   
fig = figure;


%% Plotting   
% subplot(4,2,1);
% hold on
% h = plot(X1, D1(1,:),'Color','r');
% h.DisplayName = 'Period 1';
% h = plot(X2, D2(1,:),'Color','g');
% h.DisplayName = 'Period 2';
% h = plot(X3, D3(1,:),'Color','m');
% h.DisplayName = 'Period 3';
% h = plot(X4, D4(1,:),'Color','b');
% h.DisplayName = 'Period 4';
% ylabel N1
% title (['Electrode ' electrode]);
% % % legend removed on request (Maartje)
% legend('show');
% legend('boxoff');
% legend('Location', 'Best');

% Dit is de versie met std-lijntjes
% subplot(4,2,1);
% hold on
% 
% EIdx = 1:25:length(X1i);
% h = plot(X1i, D1mean(1,:), 'Color', 'r');
% set(h, 'DisplayName', 'Period 1');
% % h = plot(X1, D1(1,:),'Color','k');
% h = errorbar(X1i(EIdx), D1mean(1,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X2i);
% h = plot(X2i, D2mean(1,:),'Color','g');
% set(h, 'DisplayName', 'Period 2');
% h = errorbar(X2i(EIdx), D2mean(1,EIdx), D2std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% h = plot(X3i, D3mean(1,:),'Color','m');
% set(h, 'DisplayName', 'Period 3');
% h = errorbar(X3i(EIdx), D3mean(1,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% h = plot(X4i, D4mean(1,:),'Color','b');
% set(h, 'DisplayName', 'Period 4');
% h = errorbar(X4i(EIdx), D4mean(1,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')

title (['Electrode ' electrode ' - ' num2str(length(subjectNrs)) ' subjects']);

subplot(4,2,1); FillPlot(X1i, X2i, X3i, X4i, D1mean(1,:), D2mean(1,:), D3mean(1,:), D4mean(1,:), D1std(1,:), D2std(1,:), D3std(1,:), D4std(1,:), 'N1 Latency', [], true)
subplot(4,2,2); FillPlot(X1i, X2i, X3i, X4i, D1mean(2,:), D2mean(2,:), D3mean(2,:), D4mean(2,:), D1std(2,:), D2std(2,:), D3std(2,:), D4std(2,:), 'N1 Amplitude', [], false)
subplot(4,2,3); FillPlot(X1i, X2i, X3i, X4i, D1mean(3,:), D2mean(3,:), D3mean(3,:), D4mean(3,:), D1std(3,:), D2std(3,:), D3std(3,:), D4std(3,:), 'P1 Latency', [], false)
subplot(4,2,4); FillPlot(X1i, X2i, X3i, X4i, D1mean(4,:), D2mean(4,:), D3mean(4,:), D4mean(4,:), D1std(4,:), D2std(4,:), D3std(4,:), D4std(4,:), 'P1 Amplitude', [], false)
subplot(4,2,5); FillPlot(X1i, X2i, X3i, X4i, D1mean(5,:), D2mean(5,:), D3mean(5,:), D4mean(5,:), D1std(5,:), D2std(5,:), D3std(5,:), D4std(5,:), 'N2 Latency', [], false)
subplot(4,2,6); FillPlot(X1i, X2i, X3i, X4i, D1mean(6,:), D2mean(6,:), D3mean(6,:), D4mean(6,:), D1std(6,:), D2std(6,:), D3std(6,:), D4std(6,:), 'N2 Amplitude', [], false)
subplot(4,2,7); FillPlot(X1i, X2i, X3i, X4i, D1mean(7,:), D2mean(7,:), D3mean(7,:), D4mean(7,:), D1std(7,:), D2std(7,:), D3std(7,:), D4std(7,:), 'P2 Latency', 'Trials', false)
subplot(4,2,8); FillPlot(X1i, X2i, X3i, X4i, D1mean(8,:), D2mean(8,:), D3mean(8,:), D4mean(8,:), D1std(8,:), D2std(8,:), D3std(8,:), D4std(8,:), 'P2 Amplitude', 'Trials', false)


% ylabel N1
% title (['Electrode ' electrode]);
% % % legend removed on request (Maartje)
% legend('show');
% legend('boxoff');
% legend('Location', 'Best');
% xlim([0 1]);

% 
% subplot(4,2,2);
% hold on
% 
% EIdx = 1:25:length(X1i);
% plot(X1i, D1mean(1,:),'Color','r');
% h = errorbar(X1i(EIdx), D1mean(2,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X2i);
% plot(X2i, D2mean(1,:),'Color','g');
% h = errorbar(X2i(EIdx), D2mean(2,EIdx), D2std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% plot(X3i, D3mean(1,:),'Color','m');
% h = errorbar(X3i(EIdx), D3mean(2,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% plot(X4i, D4mean(1,:),'Color','b');
% h = errorbar(X4i(EIdx), D4mean(2,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% xlim([0 1]);
% 
% 
% 
% subplot(4,2,3);
% hold on
% 
% EIdx = 1:25:length(X1i);
% plot(X1i, D1mean(1,:),'Color','r');
% h = errorbar(X1i(EIdx), D1mean(3,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X2i);
% plot(X2i, D2mean(1,:),'Color','g');
% h = errorbar(X2i(EIdx), D2mean(3,EIdx), D2std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% plot(X3i, D3mean(1,:),'Color','m');
% h = errorbar(X3i(EIdx), D3mean(3,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% plot(X4i, D4mean(1,:),'Color','b');
% h = errorbar(X4i(EIdx), D4mean(3,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% ylabel P1
% xlim([0 1]);
% 
% subplot(4,2,4);
% hold on
% EIdx = 1:25:length(X1i);
% plot(X1i, D1mean(4,:),'Color','r');
% h = errorbar(X1i(EIdx), D1mean(4,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 6:25:length(X2i);
% plot(X2i, D2mean(4,:),'Color','g');
% h = errorbar(X2i(EIdx), D2mean(4,EIdx), D2std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% plot(X3i, D3mean(4,:),'Color','m');
% h = errorbar(X3i(EIdx), D3mean(4,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% plot(X4i, D4mean(4,:),'Color','b');
% h = errorbar(X4i(EIdx), D4mean(4,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% xlim([0 1]);
% 
% subplot(4,2,5);
% hold on
% EIdx = 1:25:length(X1i);
% plot(X1i, D1mean(5,:),'Color','r');
% h = errorbar(X1i(EIdx), D1mean(5,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X2i);
% plot(X2i, D2mean(5,:),'Color','g');
% h = errorbar(X2i(EIdx), D1mean(5,EIdx), D1std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% plot(X3i, D3mean(5,:),'Color','m');
% h = errorbar(X3i(EIdx), D3mean(5,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% plot(X4i, D4mean(5,:),'Color','b');
% h = errorbar(X4i(EIdx), D4mean(5,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% ylabel N2
% xlim([0 1]);
% 
% 
% subplot(4,2,6);
% hold on
% EIdx = 1:25:length(X1i);
% plot(X1i, D1mean(6,:),'Color','r');
% h = errorbar(X1i(EIdx), D1mean(6,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X2i);
% plot(X2i, D2mean(6,:),'Color','g');
% h = errorbar(X2i(EIdx), D2mean(6,EIdx), D2std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% plot(X3i, D3mean(6,:),'Color','m');
% h = errorbar(X3i(EIdx), D3mean(6,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% plot(X4i, D4mean(6,:),'Color','b');
% h = errorbar(X4i(EIdx), D4mean(6,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% xlim([0 1]);
% 
% 
% subplot(4,2,7);
% hold on
% EIdx = 1:25:length(X1i);
% plot(X1i, D1mean(7,:),'Color','r');
% h = errorbar(X1i(EIdx), D1mean(7,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X2i);
% plot(X2i, D2mean(7,:),'Color','g');
% h = errorbar(X2i(EIdx), D2mean(7,EIdx), D2std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% plot(X3i, D3mean(7,:),'Color','m');
% h = errorbar(X3i(EIdx), D3mean(7,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% plot(X4i, D4mean(7,:),'Color','b');
% h = errorbar(X4i(EIdx), D4mean(7,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% ylabel P2
% xlabel 'latency'
% xlim([0 1]);
% 
% subplot(4,2,8);
% hold on
% EIdx = 1:25:length(X1i);
% plot(X1i, D1mean(8,:),'Color','r');
% h = errorbar(X1i(EIdx), D1mean(8,EIdx), D1std(1,EIdx), 'Color', 'r', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X2i);
% plot(X2i, D2mean(8,:),'Color','g');
% h = errorbar(X2i(EIdx), D2mean(8,EIdx), D2std(1,EIdx), 'Color', 'g', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X3i);
% plot(X3i, D3mean(8,:),'Color','m');
% h = errorbar(X3i(EIdx), D3mean(8,EIdx), D3std(1,EIdx), 'Color', 'm', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% EIdx = 1:25:length(X4i);
% plot(X4i, D4mean(8,:),'Color','b');
% h = errorbar(X4i(EIdx), D4mean(8,EIdx), D4std(1,EIdx), 'Color', 'b', 'LineStyle', 'none');
% set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')
% 
% xlabel 'value'
% xlim([0 1]);

end


function FillPlot(X1i, X2i, X3i, X4i, D1mean, D2mean, D3mean, D4mean, D1std, D2std, D3std, D4std, ylbl, xlbl, legend)
hold on

EIdx = 1:40:length(X1i);
h = plot(X1i, D1mean, 'Color', 'r');
set(h, 'DisplayName', 'Period 1');
h = errorbar(X1i(EIdx), D1mean(EIdx), D1std(EIdx), 'Color', 'r', 'LineStyle', 'none');
set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')

EIdx = 11:40:length(X2i);
h = plot(X2i, D2mean, 'Color','g');
set(h, 'DisplayName', 'Period 2');
h = errorbar(X2i(EIdx), D2mean(EIdx), D2std(EIdx), 'Color', 'g', 'LineStyle', 'none');
set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')

EIdx = 21:40:length(X3i);
h = plot(X3i, D3mean, 'Color','m');
set(h, 'DisplayName', 'Period 3');
h = errorbar(X3i(EIdx), D3mean(EIdx), D3std(EIdx), 'Color', 'm', 'LineStyle', 'none');
set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')

EIdx = 31:40:length(X4i);
h = plot(X4i, D4mean, 'Color','b');
set(h, 'DisplayName', 'Period 4');
h = errorbar(X4i(EIdx), D4mean(EIdx), D4std(EIdx), 'Color', 'b', 'LineStyle', 'none');
set(get(get(h,'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off')

ylabel(ylbl)
xlabel(xlbl)
% if legend
%     legend('show');
%     legend('boxoff');
%     legend('Location', 'Best');
% end
xlim([0 1]);
% ylim([-40 40]);

end